package com.codeoss.module.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.account.model.AccountTax;
import com.codeoss.module.account.service.IAccountTaxService;
import com.codeoss.module.sale.dao.SaleQuotationMapper;
import com.codeoss.module.sale.model.SaleQuotation;
import com.codeoss.module.sale.model.SaleQuotationLine;
import com.codeoss.module.sale.service.ISaleQuotationLineService;
import com.codeoss.module.sale.service.ISaleQuotationService;

@Service
public class SaleQuotationService extends BaseService<SaleQuotationMapper, SaleQuotation>
		implements ISaleQuotationService {
    @Autowired
    private ISaleQuotationLineService saleQuotationLineService;
    @Autowired
    private IAccountTaxService accountTaxService;
	@Override
	public BigDecimal calculateAmountTotal(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleQuotationLine> lineQuery=new QueryWrapper<SaleQuotationLine>();
		lineQuery.eq("sale_quotation_id", id);
		List<SaleQuotationLine> lineList=saleQuotationLineService.list(lineQuery);
		BigDecimal amountTotal=new BigDecimal(0);
		for(SaleQuotationLine line:lineList) {
			BigDecimal subTotal=line.getAmountSubtotal();
			if(subTotal!=null) {
				amountTotal=amountTotal.add(subTotal);
			}
		}
		return amountTotal;
	}

	@Override
	public BigDecimal calculateAmountUntaxed(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleQuotationLine> lineQuery=new QueryWrapper<SaleQuotationLine>();
		lineQuery.eq("sale_quotation_id", id);
		List<SaleQuotationLine> lineList=saleQuotationLineService.list(lineQuery);
		BigDecimal amountUntaxed=new BigDecimal(0);
		for(SaleQuotationLine line:lineList) {
			BigDecimal qtyOrdered=line.getQtyOrdered();
			BigDecimal total=qtyOrdered.multiply(line.getPriceEntered());
	    	amountUntaxed=amountUntaxed.add(total);
		}
		return amountUntaxed;
	}

	@Override
	public BigDecimal calculateAmountTax(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleQuotationLine> lineQuery=new QueryWrapper<SaleQuotationLine>();
		lineQuery.eq("sale_quotation_id", id);
		List<SaleQuotationLine> lineList=saleQuotationLineService.list(lineQuery);
		BigDecimal amountTax=new BigDecimal(0);
		for(SaleQuotationLine line:lineList) {
			Long taxId=line.getAccountTaxId();
			BigDecimal qtyOrdered=line.getQtyOrdered();
			BigDecimal total=qtyOrdered.multiply(line.getPriceEntered());
	    	AccountTax accountTax=accountTaxService.getById(taxId);
			BigDecimal accountRate=accountTax.getRate().divide(new BigDecimal(100));    
			amountTax=amountTax.add(total.multiply(accountRate));
		}
		return amountTax;
	}

	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleQuotationLine> lineQuery=new QueryWrapper<SaleQuotationLine>();
		lineQuery.eq("sale_quotation_id", id);
		List<SaleQuotationLine> lineList=saleQuotationLineService.list(lineQuery);
		if(lineList==null ||lineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		SaleQuotation saleQuotation=getById(id);
		saleQuotation.setApproved(true);
		update(saleQuotation);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		SaleQuotation saleQuotation=getById(id);
		saleQuotation.setApproved(false);
		update(saleQuotation);
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		SaleQuotation saleQuotation=getById(id);
		saleQuotation.setDocStatus("CO");
		update(saleQuotation);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id,String docStatus) {
		// TODO Auto-generated method stub
		SaleQuotation saleQuotation=getById(id);
		saleQuotation.setDocStatus(docStatus);
		update(saleQuotation);
	}
}